package com.example.login.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.login.R;
import com.example.login.adapter.OrderListAdapter;

import java.util.List;

import db.OrderDbHelper;
import entity.OrderInfo;
import entity.UserInfo;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link OrderFragment#} factory method to
 * create an instance of this fragment.
 */
public class OrderFragment extends Fragment {
    private View rootview;
    private RecyclerView recyclerView;
    private OrderListAdapter mOrderListAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootview=inflater.inflate(R.layout.fragment_order, container, false);
        recyclerView=rootview.findViewById(R.id.recyclerView);
        return rootview;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mOrderListAdapter=new OrderListAdapter();
        recyclerView.setAdapter(mOrderListAdapter);
        UserInfo userInfo = UserInfo.getsUserInfo();
        mOrderListAdapter.setOnItemClickListener(new OrderListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(OrderInfo orderInfo, int position) {
                new AlertDialog.Builder(getActivity())
                        .setTitle("温馨提示")
                        .setMessage("确认要删除订单吗?")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                int row=OrderDbHelper.getInstance(getActivity()).delete(orderInfo.getOrder_id()+"");
                                if(row>0){
                                    Toast.makeText(getActivity(),"删除成功",Toast.LENGTH_SHORT).show();
                                    loadData();
                                }
                                else {
                                    Toast.makeText(getActivity(),"删除失败",Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .show();
            }
        });
        if (userInfo != null) {
            List<OrderInfo> orderInfos=OrderDbHelper.getInstance(getActivity()).queryOrderListData(userInfo.getUsername());
            mOrderListAdapter.setList(orderInfos);
        }
    }

    public void loadData() {
        UserInfo userInfo=UserInfo.getsUserInfo();
        if(null!=userInfo){
            List<OrderInfo>orderInfos=OrderDbHelper.getInstance(getActivity()).queryOrderListData(userInfo.getUsername());
            mOrderListAdapter.setList(orderInfos);
        }
    }
}